package com.hmall.item.web;

import com.alibaba.fastjson.JSON;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.pojo.ItemDoc;
import com.hmall.item.pojo.MqConstants;
import com.hmall.item.service.IItemService;

import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@Component
@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 查找页面数据
     * @param page
     * @param size
     * @return
     */
    @GetMapping("list")
    public PageDTO<Item> getList(Integer page,Integer size ){
        System.out.println(page+""+size);

        PageDTO list = itemService.getList(page, size);
        return list;
    }

    @GetMapping("/{id}")
    public Item getItem(@PathVariable("id") Integer id) {
        return itemService.getItem(id);
    }

    /**
     * 新增商品
     * @param item
     */
    @PostMapping
    public void save(@RequestBody Item item){
        boolean save = itemService.save(item);
        rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE,MqConstants.HOTEL_INSERT_KEY,item.getId());

    }

    /**
     * 修改商品状态
     * @param id
     */
    @PutMapping("/status/{id}/{status}")
    public void setStatus(@PathVariable("id") Integer id,@PathVariable("status") Integer status){
        itemService.setStatus(id,status);
        rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE,MqConstants.HOTEL_INSERT_KEY,id);

    }

    /**
     * 修改商品
     * @param item
     */
    @PutMapping
    public void updataItem(@RequestBody Item item){
        itemService.updateItem(item);
        rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE,MqConstants.HOTEL_INSERT_KEY,item.getId());

    }

    /**
     * 减少存库
     * @param itemId
     * @param num
     */
    @PutMapping("/stock/{itemId}/{num}")
    public void Stock(@PathVariable("itemId") long itemId,@PathVariable("num") Integer num){
        itemService.stock(itemId,num);
    }
    //删除商品
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") Integer id ){
        itemService.delete(id);
        rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE,MqConstants.HOTEL_DELETE_KEY,id);

    }
    /**
     * 加载到所有搜索
     */
    @GetMapping("/sss")
    public void saveoverall( ) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://192.168.119.128:9200")
        ));
        BulkRequest bulkRequest = new BulkRequest();
        int page =0;
        int size =200;

        while (true){
            PageDTO<Item> list = itemService.getList(page, size);
            if (list==null){
                break;
            }
            for (Item item : list.getList()) {
                ItemDoc itemDoc = new ItemDoc(item);
                bulkRequest.add(new IndexRequest("item")
                        .id(item.getId().toString())
                        .source(JSON.toJSONString(itemDoc),XContentType.JSON));
            }
            client.bulk(bulkRequest,RequestOptions.DEFAULT);

        }
        client.close();

        System.out.println(page*size);
    }

}
